package com.java2007.xiazhaodong.hotel.servlet;

import com.java2007.xiazhaodong.hotel.entity.FoodType;
import com.java2007.xiazhaodong.hotel.factory.BeanFactory;
import com.java2007.xiazhaodong.hotel.service.FoodTypeService;
import com.java2007.xiazhaodong.hotel.service.impl.FoodTypeServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author AzureSky_X
 * @Date 2021/1/20 8:55
 * @Version 1.0
 * 菜单管理
 * 一、添加菜系
 * 二、菜系列表展示
 * 三、进入更新页面
 * 四、删除
 * 五、更新
 */
public class FoodTypeServlet extends javax.servlet.http.HttpServlet {

    private static final long serialVersionUID = 1L;
    //调用的菜系Service
    private FoodTypeService foodTypeService= BeanFactory.getInstance("foodTypeService", FoodTypeServiceImpl.class);
    //资源跳转
    private Object uri;
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        //设置编码，解决中文乱码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //获取操作类型
        String method =request.getParameter("method");
        //判断操作属于什么类型
        if ("addFoodType".equals(method)){
            //添加
            addFoodType(request,response);
        }
        else if ("list".equals(method)) {
            // 列表展示
            list(request, response);
        }

        else if ("viewUpdate".equals(method)) {
            // 进入更新页面
            viewUpdate(request, response);
        }

        else if ("delete".equals(method)) {
            // 删除
            delete(request, response);
        }

        else if ("update".equals(method)) {
            // 更新
            update(request, response);
        }
    }

    //一、添加菜系
    public void addFoodType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try{
            //1、获取请求数据封装
            String foodTypeName=request.getParameter("foodTypeName");
            FoodType ft =new FoodType();
            ft.setTypeName(foodTypeName);

            //2、调用service处理业务逻辑
            foodTypeService.save(ft);

            //3、跳转
            uri=request.getRequestDispatcher("/foodType?method=list");
        } catch (Exception e) {
            e.printStackTrace();
            uri = "/error/error.jsp";
        }
        goTo(request,response,uri);
    }
    //二、菜单列表展示
    public void list(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{

        try {
            //调用Service查询所有的类别
            List<FoodType> list=foodTypeService.getAll();
            //保存
            request.setAttribute("listFoodType",list);
            //跳转到菜系列表页面
            uri=request.getRequestDispatcher("/sys/type/foodtype_list.jap");
        } catch (Exception e) {
            e.printStackTrace();
            uri="/error/error.jsp";

        }
        //跳转
        goTo(request,response,uri);
    }

    //三、进入更新页面
    public void viewUpdate(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        try {
            // 1. 获取请求id
            String id = request.getParameter("id");
            // 2. 根据id查询对象
            FoodType ft = foodTypeService.findById(Integer.parseInt(id));
            // 3. 保存
            request.setAttribute("foodType", ft);
            // 4. 跳转
            uri = request.getRequestDispatcher("/sys/type/foodtype_update.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            uri = "/error/error.jsp";
        }

        goTo(request, response, uri);
    }

    //d. 删除
    public void delete(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        try {
            // 1. 获取请求id
            String id = request.getParameter("id");
            // 2. 调用Service
            foodTypeService.delete(Integer.parseInt(id));
            // 3. 跳转
            uri = "/foodType?method=list";
        } catch (Exception e) {
            e.printStackTrace();
            uri = "/error/error.jsp";
        }

        goTo(request, response, uri);
    }

    // e. 更新
    public void update(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        try {
            //1. 获取请求数据封装
            int id = Integer.parseInt(request.getParameter("id"));
            String name = request.getParameter("foodTypeName");
            FoodType foodType = new FoodType();
            foodType.setId(id);
            foodType.setTypeName(name);

            //2. 调用Service更新
            foodTypeService.update(foodType);
            //3. 跳转
            //list(request,response);
            uri = "/foodType?method=list";
        } catch (Exception e) {
            e.printStackTrace();
            uri = "/error/error.jsp";
        }

        // 跳转
        goTo(request, response, uri);
    }


    /**
     * 跳转的通用方法
     */
    private void goTo(HttpServletRequest request, HttpServletResponse response, Object uri)
            throws ServletException, IOException {
        if (uri instanceof RequestDispatcher){
            ((RequestDispatcher)uri).forward(request, response);
        } else if (uri instanceof String) {
            response.sendRedirect(request.getContextPath() + uri);
        }
    }





}
